import {request} from '../../utils/http'

Page({
  data: {
    price:'',
    stage:'',
    storeId:'',
    remark:'',
    pay_method:'',
    id:'',
    product:{},
    mixed_order_pre_price:'',
    selectIndex:null,
    is_scan:false
  },
  onLoad(query) {
   
    
    this.setData({
      storeId: query.storeId,
      id:query.id,
			is_scan: query.is_scan || false
    },() => {
      this.fetch(query.id)

    })
  },
  onShow(){
    // 页面显示
    my.getStorage({
      key: 'userInfo',
      success: (res) => {
        console.log(res)
        if (res.data) {
          this.setData({
            member: res.data,
          })
        }
      }
    });
  },
  onInputPrice(e){
    this.setData({
      price:e.detail.value
    }, () => {
      if (this.data.product.user_total_stage_checked == 1 || this.data.product.user_total_stage_checked == 2) {
        this.getStage(this.data.price)
      }
    })
  },
  getStage(price){
    let params = {
      price:price,
      product_id:this.data.id,
      shop_id:this.data.storeId
    }
    console.log(params)
    request('/order/get_user_stage', 'post', params).then(res => {
      console.log(res)
      if (res.code == 200) {
        let product = this.data.product
        product.user_total_stage = res.data
        this.setData({
          product,
          selectIndex:null
        })
      } else {
        my.showToast({content: res.message});
        let product = this.data.product
        product.user_total_stage = []
        this.setData({
          product,
          selectIndex:null
        })
      }
    })
  },
  onInputMixedPrice(e){
    this.setData({
      mixed_order_pre_price:e.detail.value
    })
  },
  onInputRemark(e){
    this.setData({
      remark:e.detail.value
    })
  },
  fetch(id) {
    
    request('/order/preview/' + id, 'get', {
      store_id:this.data.storeId
    }).then(res => {
      if (res.code == 200) {
        if (res.data.product.price_is_edit != 1) {
          my.redirectTo({
            url: `/pages/orderConfirm/index?id=${this.data.id}&storeId=${this.data.storeId}`
          })
        }
        this.setData({
          // price: res.data.product.price,
          stage:res.data.product.total_stage,
          pay_method:res.data.product.pay_method,
          product: res.data.product,
          // mixed_order_pre_price: res.data.product.mixed_order_pre_price
        })
      } else {
        my.showToast({content: res.message});
      }
     
    })
  },
  submit(){
    if (!this.data.price) {
      my.showToast({content: '请填写价格'})
      return
    }

    if (this.data.product.user_total_stage_checked == 1 || this.data.product.user_total_stage_checked == 2) {
      if (this.data.selectIndex == null) {
        my.showToast({content: '请选择分期数'})
        return
      }
    }

    let url = `/pages/orderConfirm/index?id=${this.data.id}&is_scan=${this.is_scan}&storeId=${this.data.storeId}&edit=true&price=${this.data.price}&remark=${this.data.remark}&stageType=${this.data.product.user_total_stage_checked}&stage=${this.data.product.user_total_stage[this.data.selectIndex]}`

    if (this.data.product.mixed_order == 1) {
      url = `${url}&mixed_price=${this.data.mixed_order_pre_price}`
    }


    my.redirectTo({
      url
    })
  },
  bindPickerChange(e){
    this.setData({
      selectIndex: e.detail.value,
    });
  },
  myOrder(){
    if (!this.data.member.mobile) {
      my.navigateTo({
        url:'/pages/login/login'
      })
      my.showToast({content: '请登录后再操作'});
      return;
    }
    debugger
    if (this.data.product.pay_method != 3) {
      my.navigateTo({
        url: '/pages/order/index?pay_method=3',
      })
    } else {
      my.navigateTo({
        url: '/pages/full-order/full-order?pay_method=1',
      })
    }
  }
});
